package thesis.filesharing.bo.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import thesis.filesharing.bo.GroupBO;
import thesis.filesharing.common.exception.functional.InvalidArgumentException;
import thesis.filesharing.dao.GroupDAO;
import thesis.filesharing.model.Group;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Service
public class GroupBOImpl implements GroupBO {
	
	@Autowired
	private GroupDAO groupDAO;
	
	@Override
	public Group findGroupStudentRegisteredInClass(Key studentKey, String classKey) {
		return groupDAO.findGroupStudentRegisteredInClass(studentKey, KeyFactory.stringToKey(classKey));
	}

	@Override
	public Group saveGroup(Group group, String[] studentIds, Key classKey) {
		if (studentIds.length <= 0) {
			throw new InvalidArgumentException();
		}
		return groupDAO.saveGroup(group, new ArrayList<String>(Arrays.asList(studentIds)), classKey);
	}

	@Override
	public boolean checkStudentRegisteredInOtherGroup(Key studentKey,
			Key classKey, Key currentGroupKey) {
		return groupDAO.checkStudentRegisteredInOtherGroup(studentKey, classKey, currentGroupKey);
	}

	@Override
	public Group updateGroup(Group group, List<String> studentIds) {
		return groupDAO.updateGroup(group, studentIds);
	}

	@Override
	public List<Group> listGroupsByClass(Key classKey) {
		return groupDAO.listGroupsByClass(classKey);
	}

	@Override
	public void deleteGroup(Key groupKey) {
		groupDAO.deleteGroup(groupKey);
	}
}
